<?php

$Agenda       = "https://www.google.com/calendar/ical/fanfare.bevel%40gmail.com/public/basic.ics";
$WebRoot      = "http://www.fanfarebevel.be/";
$DocRoot      = "/customers/8/3/6/fanfarebevel.be//httpd.www/";

$uppers       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lowers       = "abcdefghijklmnopqrstuvwxyz";

function maandString($maand) {
  if ($maand==1) {return "januari";};
  if ($maand==2) {return "februari";};
  if ($maand==3) {return "maart";};
  if ($maand==4) {return "april";};
  if ($maand==5) {return "mei";};
  if ($maand==6) {return "juni";};
  if ($maand==7) {return "juli";};
  if ($maand==8) {return "augustus";};
  if ($maand==9) {return "september";};
  if ($maand==10) {return "oktober";};
  if ($maand==11) {return "november";};
  if ($maand==12) {return "december";};
}

function datumString($begindatum = "", $einddatum = "") {
  if ($begindatum == "")
    return date("d"). " " . maandString(date("m")) . " " . date("Y");
  else {
    if ($einddatum == "") $einddatum = $begindatum;
    $beginjaar  = substr($begindatum, 0, 4);
    $eindjaar   = substr($einddatum, 0, 4);
    $beginmaand = maandString(1 * substr($begindatum, 4, 2));
    $eindmaand  = maandString(1 * substr($begindatum, 4, 2));
    $begindag   = 1 * substr($begindatum, 6, 2);
    $einddag    = 1 * substr($einddatum, 6, 2);
    if ($beginjaar == $eindjaar) {
      if ($beginmaand == $eindmaand) {
        if ($begindag == $einddag)
          return "$begindag $beginmaand $beginjaar";
        else
          return "$begindag-$einddag $eindmaand $eindjaar";
      }
      else
        return "$begindag $beginmaand-$einddag $eindmaand $eindjaar";
    }
    else
      return "$begindag $beginmaand $beginjaar-$einddag $eindmaand $eindjaar";
  }
}


abstract class XMLContent {
  protected $file;
  protected $xml;
  protected $xpath;
  protected $simplexml;
  protected $protocol = "file";
  protected $changed = false;

  function __construct() {
    global $DocRoot;

    $this->xml       = new DOMDocument();
    $this->xml->load($this->file);
    $this->xpath     = new DOMXPath($this->xml);
    $this->simplexml = simplexml_import_dom($this->xml);
    foreach ($this->simplexml->getDocNamespaces() as $namespace => $uri)
      $this->simplexml->registerXPathNamespace($namespace, $uri);
    $this->simplexml->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
  }

  function __destruct() {
    if ($this->changed && ($this->protocol == "file")) {
      $this->xml->save($this->file);
    }
  }

  function reload_simplexml() {
    $this->simplexml = simplexml_import_dom($this->xml);
  }
}


class ErrorTable extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;
    $this->file = $DocRoot . "errors.xml";
    parent::__construct();
  }

  function unknown($error) {
    $query = $this->simplexml->xpath('/errors/error[@code="'.$error.'"]');
    return (empty($query));
  }

  function lookup($error) {
    if (! $this->unknown($error))
      $query = $this->simplexml->xpath('/errors/error[@code="'.$error.'"]');
    else
      $query = $this->simplexml->xpath('/errors/default');
    return (string) $query[0];
  }
}


class OrkestInfo extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;
    $this->file = $DocRoot . "orkest/orkest.xml";
    parent::__construct();
  }

  function lezen() {
    $result = Array();
    $query = $this->simplexml->xpath('/orkest/instrument');

    foreach ($query as $xmlnode) {
      $naam   = (string) $xmlnode['naam'];
      $aantal = (string) $xmlnode['aantal'];
      $result[$naam] = $aantal;
    }

    return $result;
  }

  function aantal_muzikanten() {
    $result = 0;
    foreach ($this->lezen() as $aantal) $result += $aantal;
    return $result;
  }
}


class BestuursInfo extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;
    $this->file = $DocRoot . "bestuur/bestuur.xml";
    parent::__construct();
  }

  function lezen() {
    $result = Array();
    $query  = $this->simplexml->xpath('/bestuur/bestuurslid');
    foreach ($query as $xmlnode) {
      $taken = Array();
      foreach ($xmlnode->taken->taak as $taak) {
        array_push($taken, (string) $taak);
      }

      array_push($result,
        Array(
          "naam"     => (string) $xmlnode->naam,
          "taken"    => $taken
        )
      );
    }
    return $result;
  }

  function voorzitter() {
    $result = Array();
    $query  = $this->simplexml->xpath('/bestuur/bestuurslid[taken/taak="Voorzitter"]');
    foreach ($query as $xmlnode)
      array_push($result,
        Array(
          "naam"     => (string) $xmlnode->naam,
          "straat"   => (string) $xmlnode->straat,
          "gemeente" => (string) $xmlnode->gemeente,
          "telefoon" => (string) $xmlnode->telefoon,
          "email"    => (string) $xmlnode->email
        )
      );
    return $result;
  }

  function secretaris() {
    $result = Array();
    $query  = $this->simplexml->xpath('/bestuur/bestuurslid[taken/taak="Secretaris"]');
    foreach ($query as $xmlnode)
      array_push($result,
        Array(
          "naam"     => (string) $xmlnode->naam,
          "straat"   => (string) $xmlnode->straat,
          "gemeente" => (string) $xmlnode->gemeente,
          "telefoon" => (string) $xmlnode->telefoon,
          "email"    => (string) $xmlnode->email
        )
      );
    return $result;
  }

}


class ConcertenLijst extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;
    $this->file = $DocRoot . "concerten/concerten.xml";
    parent::__construct();
  }

  function lijst($aantal = 0) {
    $result = Array();
    $query  = $this->simplexml->xpath('/concerten/concert[position() <= '.$aantal.']');
    foreach ($query as $xmlnode) {
      array_push($result,
        Array(
          "naam"    => (string) $xmlnode['naam'],
          "datum"   => (string) $xmlnode['datum'],
          "jaar"    => (string) $xmlnode['jaar'],
          "locatie" => (string) $xmlnode['locatie']
        )
      );
    }
    return $result;
  }

  function concertNamen() {
    $result = Array();
    $query  = $this->simplexml->xpath('/concerten/concert');
    foreach ($query as $xmlnode)
      array_push($result,
        (string) $xmlnode['naam']
      );
    return array_unique($result);
  }
  
  function programma($naam, $locatie, $jaar) {
    $result = Array();
    $query = $this->simplexml->xpath('/concerten/concert[@naam="'.$naam.'" and @locatie="'.$locatie.'" and @jaar="'.$jaar.'"]/nummer');
    foreach ($query as $xmlnode)
      array_push($result,
        Array(
          "titel"       => (string) $xmlnode,
          "componist"   => (string) $xmlnode['componist'],
          "arrangeur"   => (string) $xmlnode['arrangeur'],
          "opmerkingen" => (string) $xmlnode['opmerkingen']
        )
      );
    return $result;
  }

  function concerten($namen, $beginjaar, $eindjaar) {
    $result = Array();
    for ($jaar = $beginjaar; $jaar <= $eindjaar; $jaar++) {
      foreach ($namen as $naam) {
        $locaties = Array();
        $query = $this->simplexml->xpath('/concerten/concert[@naam="'.$naam.'" and @jaar="'.$jaar.'"]/@locatie');
        foreach ($query as $locatie) {
          array_push($result, 
            Array(
              "naam"    => (string) $naam,
              "jaar"    => (string) $jaar,
              "locatie" => (string) $locatie
            )
          );
        }
      }
    }

    return $result;
  }

  function stukken($titel, $componist) {
    global $uppers, $lowers;
    $result = Array();
    $query = $this->simplexml->xpath('//nummer[(contains(translate(@arrangeur, "'.$uppers.'", "'.$lowers.'"), translate("'.$componist.'", "'.$uppers.'", "'.$lowers.'")) or 
                                                contains(translate(@componist, "'.$uppers.'", "'.$lowers.'"), translate("'.$componist.'", "'.$uppers.'", "'.$lowers.'"))) and 
                                               contains(translate(string(.),  "'.$uppers.'", "'.$lowers.'"), translate("'.$titel.'",     "'.$uppers.'", "'.$lowers.'"))]');
    foreach ($query as $xmlnode) {
      $parents = $xmlnode->xpath('..');
      foreach ($parents as $parent) {
        array_push($result,
          Array(
            "titel"          => (string) $xmlnode,
            "componist"      => (string) $xmlnode['componist'],
            "arrangeur"      => (string) $xmlnode['arrangeur'],
            "opmerkingen"    => (string) $xmlnode['opmerkingen'],
            "concertnaam"    => (string) $parent['naam'],
            "concertjaar"    => (string) $parent['jaar'],
            "concertlocatie" => (string) $parent['locatie']
          )
        );
      }
    }
    return $result;
  }
}


class Kalender {
  protected $file;

  function __construct() {
    global $Agenda;
    $this->file = $Agenda;
  }

  function lezen() {
    $result = Array();
    $ical = file_get_contents($this->file);
    preg_match_all('/(BEGIN:VEVENT.*?END:VEVENT)/si', $ical, $result, PREG_PATTERN_ORDER);
    for ($i = 0; $i < count($result[0]); $i++) {
      $tmpbyline = explode("\r\n", $result[0][$i]);

      foreach ($tmpbyline as $item) {
        $tmpholderarray = explode(":",$item);
        if (count($tmpholderarray) >1) { 
          $majorarray[$tmpholderarray[0]] = $tmpholderarray[1];
        }                
      }

      if (preg_match('/DESCRIPTION:(.*)END:VEVENT/si', $result[0][$i], $regs)) {
        $majorarray['DESCRIPTION'] = str_replace("  ", " ", str_replace("\r\n", "", $regs[1]));
      } 
      $icalarray[] = $majorarray;
      unset($majorarray);             
    }
    $events = $icalarray;

    $timezone = "Europe/Brussels";
    date_default_timezone_set($timezone);

    foreach($events as $event){
      if (isSet($event['RRULE'])) continue;
      $startdate = date('Ymd', strtotime(str_replace('T', '', str_replace('Z', '', $event['DTSTART']))));
      $enddate = date('Ymd', strtotime(str_replace('T', '', str_replace('Z', '', $event['DTEND']))));

      $activiteit = Array(
          "begindatum" => (string) $startdate,
          "einddatum"  => (string) $enddate,
          "naam"       => (string) $event['SUMMARY'],
          "plaats"     => (string) $event['LOCATION']
      );
      array_push($result, $activiteit);
    };
    sort($result);
    return $result;
  }
}


class Fotoboek extends XMLContent {

  function __construct() {
    $this->protocol = "http";
    $this->file = "http://picasaweb.google.com/data/feed/api/user/fanfare.bevel?kind=album";
    parent::__construct();
  }

  function overzicht() {
    $result = Array();
    $query  = $this->simplexml->xpath('//feed:entry');

    foreach ($query as $reeks) {
      $id = $reeks->xpath('./gphoto:name');
      array_push($result, Array(
          "dir"  => (string) $id[0],
          "naam" => (string) $reeks->title,
          "jaar" => substr((string) $reeks->published, 0, 4)
        )
      );
    };
    return $result;
  }
}


class FotoboekReeks extends XMLContent {
  private $boeknaam;
  private $boektitel;

  function __construct($boeknaam = "") {
    $this->protocol = "http";
    if ($boeknaam != "") 
      $this->file = "http://picasaweb.google.com/data/feed/api/user/fanfare.bevel/album/$boeknaam?kind=photo&thumbsize=144,640";
    parent::__construct();

    $titel           = $this->simplexml->xpath('.//feed:title');
    $this->boeknaam  = $boeknaam;
    $this->boektitel = (string) $titel[0];
  }

  function vraagfotos($begin, $end) {
    $result = Array();
    $query  = $this->simplexml->xpath('//feed:entry[position() >= '.$begin.' and position() <= '.$end.']');

    foreach ($query as $foto) {
      $content   = $foto->xpath('.//media:thumbnail[2]/@url');
      $thumbnail = $foto->xpath('.//media:thumbnail[1]/@url');
      $fa = Array (
        "foto"      => (string) $content[0],
        "thumbnail" => (string) $thumbnail[0]
      );

      array_push($result, $fa);
    }

    return $result;
  }

  function lengte() {
    $query = $this->simplexml->xpath('//feed:entry');
    return count($query);
  }

  function boeknaam() {
    return $this->boeknaam;
  }

  function boektitel() {
    return $this->boektitel;
  }
}

class Links extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;
    $this->file = $DocRoot . "links/links.xml";
    parent::__construct();
  }

  function uitgebreidLinks() {
    $result = Array();
    $query  = $this->simplexml->xpath('/links/link[*]');
    foreach ($query as $link) { 
      array_push($result, Array(
        "naam"     => (string) $link['naam'],
        "href"     => (string) $link['href'],
        "tekening" => ($link->tekening) ? (string) $link->tekening['src'] : "",
        "tekst"    => ($link->tekst) ? (string) $link->tekst : ""
      ));
    }
    return $result;
  }

  function kortLinks() {
    $result = Array();
    $query  = $this->simplexml->xpath('/links/link[count(*) = 0]');
    foreach ($query as $link) {
      array_push($result, Array(
        "naam"     => (string) $link['naam'],
        "href"     => (string) $link['href']
      ));
    }
    return $result;
  }

}


class Menu extends XMLContent {

  function __construct() {
    global $DocRoot, $WebRoot;

    $this->file = $DocRoot . "menu.xml";
    parent::__construct();
  }

  function output($html, $body) {
    global $DocRoot, $WebRoot;

    $div  = $html->createElement("div");

    $div->setAttribute("class", "menu");
    $query = $this->simplexml->xpath('/menu/item');

    foreach ($query as $xmlnode) {
      $a = $html->createElement("a", $xmlnode['naam']);
      $a->setAttribute("href", $WebRoot.$xmlnode['href']);
      $div->appendChild($a);
      $div->appendChild($html->createElement("br"));
    }

    $body->appendChild($div);
  }

}


class WebPagina {
  protected $html;
  protected $contents;
  protected $xpath;
  private $title;
  private $menu;
  private $body;

  function __construct($titel = "", $file = "") {
    global $DocRoot, $WebRoot;

    $impl           = new DomImplementation();
    $dtd            = $impl->createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
    $this->html     = $impl->createDocument("", "", $dtd);
    $this->title    = ($titel == "")? "Koninklijke Fanfare Sint-Cecilia Bevel" : $titel;

    $root           = $this->html->appendChild($this->html->createElement("html"));
    $head           = $root->appendChild($this->html->createElement("head"));
    $link           = $head->appendChild($this->html->createElement("link"));
    $title          = $head->appendChild($this->html->createElement("title", htmlentities("Koninklijke Fanfare Sint-Cecilia Bevel: " . $this->title)));
    $this->body     = $root->appendChild($this->html->createElement("body"));

    $link->setAttribute("rel", "stylesheet");
    $link->setAttribute("title", "fanfare");
    $link->setAttribute("href", $WebRoot . "default.css");
    $link->setAttribute("type", "text/css");
    $link->setAttribute("media", "screen");

    $this->menu = new Menu();

    $div = $this->body->appendChild($this->html->createElement("div"));
    $div->setAttribute("class", "headerpane");
    $a = $div->appendChild($this->html->createElement("a"));
    $logo = $a->appendChild($this->html->createElement("img"));
    $a->setAttribute("href", $WebRoot);
    $logo->setAttribute("src", $WebRoot . "images/logo.png");
    $logo->setAttribute("alt", "Koninklijke Fanfare Sint-Cecilia Bevel");
    $logo->setAttribute("style" , "margin:5px; margin-left: 15px; width: 92px; height: 110px; float: left;"); 
    $header = $div->appendChild($this->html->createElement("h1", "Koninklijke Fanfare Sint-Cecilia Bevel"));

    $this->menu->output($this->html, $this->body);

    $this->contents = $this->body->appendChild($this->html->createElement("div"));
    $this->contents->setAttribute("class", "contentpane");
    $title = $this->contents->appendChild($this->html->createElement("h2", $this->title));

    if ($file != "") {
      $page = new DOMXPath(DOMDocument::load($DocRoot . $file));
      foreach ($page->query("/*/node()") as $node) {
        $x = $this->html->importNode($node, true);
        $this->contents->appendChild($x);
      }
    }

    $this->xpath = new DOMXPath($this->html);
  }

  function changeTitle($title) {
    $this->title = $title;
    $head        = $this->body->getElementsByTagname('h2');
    $head->item(0)->parentNode->replaceChild($this->html->createElement('h2', $title), $head->item(0));
  }

  function __destruct() {
    print($this->html->saveXML());
  }

}


class ErrorPagina extends WebPagina {
  private $errortable;
  
  function __construct($error) {
    global $DocRoot, $WebRoot;

    $this->errortable = new ErrorTable();
    if (! $this->errortable->unknown($error))
      parent::__construct("Probleem $error");
    else
      parent::__construct("Ongekend probleem");

    $this->contents->appendChild($this->html->createElement("p", $this->errortable->lookup($error)));
  }
}


class ConcertenPagina extends WebPagina {
  private $concertenlijst;

  function __construct() {
    global $DocRoot, $WebRoot;

    $this->concertenlijst = new ConcertenLijst();
    parent::__construct("Concerten");
  }

  function menu() {
    global $DocRoot, $WebRoot;

    foreach ($this->concertenlijst->lijst(1) as $key => $concert) {
      $naam = $concert['naam'];
      $jaar = $concert['jaar'];
      $locatie = $concert['locatie'];
      $p = $this->contents->appendChild($this->html->createElement("h3", "$naam $jaar ($locatie)"));
      $p = $this->contents->appendChild($this->html->createElement("p", "Het " . $naam . " in " . $locatie . " gaat dit jaar door op " . $concert['datum'] . " " . $jaar . ". "));
      $p->appendChild($this->html->createTextNode("We hopen op u daar aan te mogen treffen. "));
      $p = $this->contents->appendChild($this->html->createElement("p", "Hieronder vindt u alvast het programma:"));

      $table = $this->contents->appendChild($this->html->createElement("table"));
      $table->setAttribute("style", "margin-left:auto; margin-right:auto;");
      foreach ($this->concertenlijst->programma($naam, $locatie, $jaar) as $i => $nummer) {
        if ($nummer['arrangeur'] != "") $nummer['arrangeur'] = "Arr. " . $nummer['arrangeur']; 
        $row = $table->appendChild($this->html->createElement("tr"));
        $col = $row->appendChild($this->html->createElement("th", ($i+1)."."));
        $col = $row->appendChild($this->html->createElement("td", $nummer['titel']));
        $col = $row->appendChild($this->html->createElement("td", $nummer['componist']));
        $col = $row->appendChild($this->html->createElement("td", $nummer['arrangeur']));
        if ($nummer['opmerkingen'] != "") {
          $row = $table->appendChild($this->html->createElement("tr"));
          $col = $row->appendChild($this->html->createElement("td", " "));
          $col = $row->appendChild($this->html->createElement("td", $nummer['opmerkingen']));
          $col->setAttribute("colspan", "3");
        }
      }
    }
  }

  function toonConcert($naam, $locatie, $jaar) {
    $p     = $this->contents->appendChild($this->html->createElement("h3", "$naam $jaar ($locatie)"));
    $table = $this->contents->appendChild($this->html->createElement("table"));
    $table->setAttribute("style", "margin-left:auto; margin-right:auto;");
    foreach ($this->concertenlijst->programma($naam, $locatie, $jaar) as $i => $nummer) {
      if ($nummer['arrangeur'] != "") $nummer['arrangeur'] = "Arr. " . $nummer['arrangeur']; 
      $row = $table->appendChild($this->html->createElement("tr"));
      $col = $row->appendChild($this->html->createElement("th", ($i+1)."."));
      $col = $row->appendChild($this->html->createElement("td", $nummer['titel']));
      $col = $row->appendChild($this->html->createElement("td", $nummer['componist']));
      $col = $row->appendChild($this->html->createElement("td", $nummer['arrangeur']));
      if ($nummer['opmerkingen'] != "") {
        $row = $table->appendChild($this->html->createElement("tr"));
        $col = $row->appendChild($this->html->createElement("td", " "));
        $col = $row->appendChild($this->html->createElement("td", $nummer['opmerkingen']));
        $col->setAttribute("colspan", "3");
      }
    }
  }

}


class ContactPagina extends WebPagina {
  private $bestuursinfo;

  function __construct() {
    global $DocRoot, $WebRoot;

    $this->bestuursinfo = new BestuursInfo();
    parent::__construct("Contact opnemen");

    $p  = $this->html->createElement("p", "Wens je contact te nemen met onze fanfare, gelieve dit dan te doen via de volgende personen : ");
    $ul = $this->html->createElement("ul");

    $this->contents->appendChild($p);
    $this->contents->appendChild($ul);

    foreach ($this->bestuursinfo->voorzitter() as $voorzitter) {
      $li    = $this->html->createElement("li", "Voorzitter:");
      $br    = $this->html->createElement("br");
      $table = $this->html->createElement("table");
      
      foreach ($voorzitter as $what => $item) {
        $row  = $this->html->createElement("tr");
        $col1 = $this->html->createElement("td", "");
        if ($what != "email") {
          $col2 = $this->html->createElement("td", $item);
        } else {
          $col2  = $this->html->createElement("td");
          $email = $this->html->createElement("a", $item);
          $email->setAttribute("href", "mailto:".$item);
          $col2->appendChild($email);
        }
        $row->appendChild($col1);
        $row->appendChild($col2);
        $table->appendChild($row);
      }

      $li->setAttribute("style", "font-style:italic;font-size:14px;");
      $li->appendChild($br);
      $li->appendChild($table);
      $ul->appendChild($li);
    }

    foreach ($this->bestuursinfo->secretaris() as $secretaris) {
      $li    = $this->html->createElement("li", "Secretaris:");
      $br    = $this->html->createElement("br");
      $table = $this->html->createElement("table");

      foreach ($secretaris as $what => $item) {
        $row  = $this->html->createElement("tr");
        $col1 = $this->html->createElement("td", "");
        if ($what != "email") {
          $col2 = $this->html->createElement("td", $item);
        } else {
          $col2  = $this->html->createElement("td");
          $email = $this->html->createElement("a", $item);
          $email->setAttribute("href", "mailto:".$item);
          $col2->appendChild($email);
        }
        $row->appendChild($col1);
        $row->appendChild($col2);
        $table->appendChild($row);
      }

      $li->setAttribute("style", "font-style:italic;font-size:14px;");
      $li->appendChild($br);
      $li->appendChild($table);
      $ul->appendChild($li);
    }
  }
}


class KalenderPagina extends WebPagina {
  private $kalender;

  function __construct() {
    $this->kalender = new Kalender();
    parent::__construct("Kalender");
  }

  function menu() {
    global $WebRoot;
    $intro = $this->html->createElement("p", "Volgende activiteiten hebben we voor de nabije toekomst gepland:");
    $this->contents->appendChild($intro);

    $table = $this->html->createElement("table");
    $table->setAttribute("width", "100%");

    foreach ($this->kalender->lezen() as $id => $activiteit) {
      if (!isSet($activiteit['einddatum'])) continue;
      if ($activiteit['einddatum'] >= date("Ymd")) {
      $row  = $this->html->createElement("tr");
      if ($activiteit['begindatum'] == $activiteit['einddatum'])
        $col1 = $this->html->createElement("td", datumString($activiteit['begindatum']));
      else
        $col1 = $this->html->createElement("td", datumString($activiteit['begindatum'], $activiteit['einddatum']));
      $col2 = $this->html->createElement("td", $activiteit['naam']);
      $col3 = $this->html->createElement("td", $activiteit['plaats']);

      $table->appendChild($row);
      $row->appendChild($col1);
      $row->appendChild($col2);
      $row->appendChild($col3);
      $col1->setAttribute("style", "text-align:right;padding-right:15px;");
      $col3->setAttribute("style", "font-style:italic;");
    }
    else {}
    }

    $this->contents->appendChild($table);
  }
}


class FotoboekPagina extends WebPagina {
  private $fotoboek;
  private $fotoboekreeks;
  private $aantalKolommen = 4;
  private $aantalRijen    = 5;

  function __construct($boeknaam = "") {
    if ($boeknaam == "") {
      $this->fotoboek      = new Fotoboek();
      parent::__construct("Fotoboek");
    } else {
      $this->fotoboekreeks = new FotoboekReeks($boeknaam);
      parent::__construct("Fotoboek: " . $this->fotoboekreeks->boektitel());
    }
  }

  function menu() {
    global $WebRoot, $DocRoot;

    $jaar = "";
    foreach ($this->fotoboek->overzicht() as $reeks) {
      if ($reeks['jaar'] != $jaar) {
        $h3 = $this->contents->appendChild($this->html->createElement("h3", $reeks['jaar']));
        $ul = $this->contents->appendChild($this->html->createElement("ul"));
        $jaar = $reeks['jaar'];
      }
      $li = $ul->appendChild($this->html->createElement("li"));
      $a  = $li->appendChild($this->html->createElement("a", $reeks['naam']));
      $a->setAttribute("href", $WebRoot . "fotoboek/index.php?fotoboek=" . $reeks['dir']);
    }
  }

  function toonreeks($pagina) {
    global $WebRoot, $DocRoot;
    $div = $this->contents->appendChild($this->html->createElement("div"));
    $div->setAttribute("style", "text-align:justify");

    if ($pagina > 0) {
      $minPos = ($pagina - 1)*($this->aantalKolommen*$this->aantalRijen) + 1;
      $maxPos = $pagina*$this->aantalKolommen*$this->aantalRijen;
    } else {
      $pagina = 1;
      $minPos = 1;
      $maxPos = $this->aantalKolommen*$this->aantalRijen;
    }
    if ($maxPos > $this->fotoboekreeks->lengte())
      $maxPos = $this->fotoboekreeks->lengte();

    $reeks = $this->fotoboekreeks->vraagfotos($minPos, $maxPos);
    $table = $this->contents->appendChild($this->html->createElement("table"));
    $table->setAttribute("style", "margin-left:auto;margin-right:auto;");
    $slideshowRow = $table->appendChild($this->html->createElement("tr"));
    $slideshowCell = $slideshowRow->appendChild($this->html->createElement("td"));
    $slideshowCell->setAttribute("colspan", $this->aantalKolommen);
    $slideshowCell->setAttribute("style", "text-align:center;");
    $slideshowLink = $slideshowCell->appendChild($this->html->createElement("a", "[slideshow]"));
    $slideshowLink->setAttribute("href", "http://picasaweb.google.com/fanfare.bevel/".$this->fotoboekreeks->boeknaam()."#slideshow");
    $slideshowLink->setAttribute("target", "_blank");

    $i = 0;
    for ($pos = $minPos; $pos <= $maxPos; $pos++) {
      if ($i % $this->aantalKolommen == 0) {
        $row = $table->appendChild($this->html->createElement("tr"));
      }
       
      $col  = $row->appendChild($this->html->createElement("td"));
      $link = $col->appendChild($this->html->createElement("a"));
      $img  = $link->appendChild($this->html->createElement("img"));
      $img->setAttribute("src", $reeks[$i++]['thumbnail']);
      $img->setAttribute("alt", "Klik om foto te vergroten");
      $link->setAttribute("href", $WebRoot . "fotoboek/index.php?fotoboek=".$this->fotoboekreeks->boeknaam()."&foto=".$pos);
      $col->setAttribute("style", "text-align:center;");
    }


   $aantalPaginas = ceil($this->fotoboekreeks->lengte() / ($this->aantalKolommen*$this->aantalRijen))+1;

    $browsebar = $this->html->createElement("div", "Pagina: ");
    $browsebar = $this->contents->appendChild($browsebar);
    $browsebar->setAttribute("id", "browsebar");

    for ($j = 1; $j < $aantalPaginas; $j++) {
      if ($j == $pagina) {
        $p = $this->html->createElement("b", $j);
        $browsebar->appendChild($p);
      } else {
        $p    = $this->html->createElement("a", $j);
        $p->setAttribute("href", $WebRoot . "fotoboek/index.php?fotoboek=".$this->fotoboekreeks->boeknaam()."&pagina=$j");
        $browsebar->appendChild($p);
      }
      $whitespace = $this->html->createTextNode(" ");
      $browsebar->appendChild($whitespace);
    }
  }

  function toonfoto($filename) {
    global $DocRoot, $WebRoot;

    // Toon navigatiebalk
    $pagina  = floor(($filename - 1)/($this->aantalKolommen * $this->aantalRijen))+1;
    $laatste = $this->fotoboekreeks->lengte();
    $url     = $WebRoot . "fotoboek/index.php?fotoboek=" . $this->fotoboekreeks->boeknaam() . "&pagina=" . $pagina;
    $navig  = $this->contents->appendChild($this->html->createElement("div"));
    $navig->setAttribute("id", "browsebar");
    $table  = $navig->appendChild($this->html->createElement("table"));
    $row    = $table->appendChild($this->html->createElement("tr"));
    $col    = $row->appendChild($this->html->createElement("td"));
    $link   = $col->appendChild($this->html->createElement("a", "eerste"));
    $link->setAttribute("href", $url . "&foto=1");
    $row->setAttribute("style", "text-align:center;");
    $col    = $row->appendChild($this->html->createElement("td"));
    $link   = $col->appendChild($this->html->createElement("a", "vorige"));
    $link->setAttribute("href", $url . "&foto=" . (($filename > 1) ? $filename - 1 : 1));
    $row->setAttribute("style", "text-align:center;");
    $col    = $row->appendChild($this->html->createElement("td"));
    $link   = $col->appendChild($this->html->createElement("a", "index"));
    $link->setAttribute("href", $url);
    $row->setAttribute("style", "text-align:center;");
    $col    = $row->appendChild($this->html->createElement("td"));
    $link   = $col->appendChild($this->html->createElement("a", "volgende"));
    $link->setAttribute("href", $url . "&foto=" . (($filename < $laatste) ? $filename + 1 : $laatste));
    $row->setAttribute("style", "text-align:center;");
    $col    = $row->appendChild($this->html->createElement("td"));
    $link   = $col->appendChild($this->html->createElement("a", "laatste"));
    $link->setAttribute("href", $url . "&foto=$laatste");
    $row->setAttribute("style", "text-align:center;");
    $table->setAttribute("style", "width:100%");

    // De eigenlijke foto
    $foto = $this->fotoboekreeks->vraagfotos($filename, $filename);
    $div  = $this->contents->appendChild($this->html->createElement("div"));
    $div->setAttribute("style", "text-align:center;");
    $img  = $div->appendChild($this->html->createElement("img"));
    $img->setAttribute("src", $foto[0]['foto']);
    $img->setAttribute("alt", $foto[0]['foto']);
  }
}

?>
